package com.wonderGirls.swGirls2;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.kuguo.ad.PushAdsManager;
import com.mobclick.android.MobclickAgent;
import com.wonderGirls.swGirls2.offer.OfferHelper;
import com.wonderGirls.swGirls2.util.GirlItemAdapter;
import com.wonderGirls.swGirls2.util.ResourceManager;

public class MainActivity extends Activity {
	ViewFlipper vfHome;
	ListView lv;
	Timer timer;

	TextView txtGold;
	Button btnGetGold, btnUnlockAll;

	boolean isCurrent = true;
	boolean isLoaded = false;

	public GirlItemAdapter itemAdapter;
	public static MainActivity instance;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// 接收来自图片下载器的消息
			if (msg.what == 77) {
				vfHome.setInAnimation(AnimationUtils.loadAnimation(
						MainActivity.this, R.anim.animation_in));
				vfHome.setOutAnimation(AnimationUtils.loadAnimation(
						MainActivity.this, R.anim.animation_out));
				vfHome.showNext();
			} else if (msg.what == 78) {

			}
		}
	};

	protected void onStop() {
		super.onStop();
		isCurrent = false;
	};

	@Override
	protected void onRestart() {
		super.onRestart();
		isCurrent = true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		isCurrent = false;
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		isCurrent = true;
		MobclickAgent.onResume(this);
		if (isLoaded) {
			OfferHelper.updateCoins(this);
		}
	}

	@Override
	protected void onDestroy() {
		OfferHelper.dispose(this);
		super.onDestroy();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		ResourceManager.init(this);
		MobclickAgent.onError(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		if (!isLoaded) {
			isLoaded = true;
			instance = this;
			vfHome = (ViewFlipper) findViewById(R.id.vfHome);
			lv = (ListView) findViewById(R.id.listGirl);
			timer = new Timer();
			OfferHelper.init(this);
			initUI();
		}
	}

	private void initUI() {
		// 首页轮播内容
		for (int i = 0; i < ResourceManager.PageCount; i++) {
			try {
				View page = this.getLayoutInflater().inflate(R.layout.homeshow,
						null);
				ImageView iv = (ImageView) page
						.findViewById(R.id.imageViewHome);
				String fileName = ResourceManager.PageDir + File.separator
						+ (i + 1) + ".jpg";
				iv.setImageBitmap(ResourceManager.getBitmapFromAsset(fileName));
				vfHome.addView(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		vfHome.showNext();
		timer.schedule(new HomeTask(), 5000, 8000);

		// 设置List
		lv = (ListView) this.findViewById(R.id.listGirl);
		itemAdapter = new GirlItemAdapter(this);
		lv.setAdapter(itemAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 首先判断是否上锁
				if (!ResourceManager.indexToIsLockedMap.get(arg2)) {
					Intent i = new Intent(MainActivity.this,
							GirlGalleryActivity.class);
					i.putExtra("girlIndex", arg2);
					MainActivity.this.startActivity(i);
				} else {
					dialog("提示", "请用金币购买该图集");
				}
			}
		});

		txtGold = (TextView) this.findViewById(R.id.txtGold);
		updateGold();

		btnGetGold = (Button) this.findViewById(R.id.btnGetGold);
		btnGetGold.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				OfferHelper.showOffer(MainActivity.instance, null, null);
			}
		});

		btnUnlockAll = (Button) this.findViewById(R.id.btnUnlockAll);
		btnUnlockAll.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ResourceManager.unlockAll();
				MainActivity.instance.itemAdapter.notifyDataSetChanged();
			}
		});
	}

	/**
	 * 更新金币
	 */
	public void updateGold() {
		txtGold.setText(ResourceManager.currentGold + "");
	}

	public void dialog(String title, final String message) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("知道了", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	class HomeTask extends TimerTask {

		@Override
		public void run() {
			if (isCurrent) {
				handler.sendEmptyMessage(77);
			}
		}

	}

	public void showExitTips() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog alertDialog = new AlertDialog.Builder(
						MainActivity.this)
						.setTitle("提示")
						.setMessage("是否退出")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										// 酷果
										PushAdsManager paManager = PushAdsManager
												.getInstance();
										paManager
												.setCooId(MainActivity.this,
														"d6800edcc29544dda75f6ff841030807");
										paManager.receivePushMessage(
												MainActivity.this, true);

										MainActivity.this.finish();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								}).setCancelable(false).create(); // 创建对话框
				alertDialog.show();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			showExitTips();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
